module.exports = {
  injectedJavaScript: `
(function () {
    console.log("开始注入js");
    if (WebViewBridge) {
        WebViewBridge.promises = {};

        WebViewBridge.initRN = function () {
            const guid = function () {
                function s4() {
                    return Math.floor((1 + Math.random()) * 0x10000).toString(16).substring(1);
                };

                return s4() + s4() + '-' + s4() + '-' + s4() + '-' + s4() + '-' + s4() + s4() + s4();
            };

            window.webViewBridge = Object.assign({},window.webViewBridge,{
                send: function (data) {
                    var msgObj = data;
                    msgObj.id = guid();

                    if (msgObj.name && msgObj.name !== 'fromBridge.call.over' && msgObj.name !== 'webViewBridge.send.over') {
                        return new Promise(function (resolve, reject) {
                            WebViewBridge.promises[msgObj.id] = { resolve: resolve, reject: reject };
                            WebViewBridge.send(JSON.stringify(msgObj));
                        });
                    }
                    else {
                        WebViewBridge.send(JSON.stringify(msgObj));
                    }
                },
                call: function (fnName, data) {
                    var msgObj = {
                        fnName: fnName,
                        data: data || {},
                        id: guid(),
                    };

                    var msg = JSON.stringify(msgObj);

                    return new Promise(function (resolve, reject) {
                        WebViewBridge.promises[msgObj.id] = { resolve: resolve, reject: reject };
                        WebViewBridge.send(msg);
                    });
                },
            });
            WebViewBridge.onMessage = function (e) {
                console.log(e);
                var message;
                try {
                    message = JSON.parse(e);
                }
                catch (err) {
                    console.log('WebViewBridge.onMessage error');
                    console.log(err);
                    return;
                }

                if (message.originalArgs) {
                    if (WebViewBridge.promises[message.originalArgs.id]) {
                        if (message.isSuccess) {
                            WebViewBridge.promises[message.originalArgs.id].resolve(message.result);
                        } else {
                            WebViewBridge.promises[message.originalArgs.id].reject(message.error);
                        }
                        delete WebViewBridge.promises[message.originalArgs.id];
                    }
                }
                else if (message.fnName) {
                    if(window.sogFormBridge){
                      eval('window.sogFormBridge.call').apply(this, [message.fnName, message]);
                    }else{
                      var eventMessage = {
                                name: 'formBridge.call.over',
                                originalArgs: message,
                                isSuccess: false,
                                error: '海鸥2 sogFormBridge 未注入方法：'+message.fnName,
                            };
                      WebViewBridge.send(JSON.stringify(eventMessage));
                    }
                }
                else {
                    if (window.webViewBridge && window.webViewBridge.onMessage) {
                        window.webViewBridge.onMessage(message);
                    }
                }
            };
        };
        WebViewBridge.initRN();
    }
}());
`,
}
